package com.bsoft.hospitalchs.ui.childrecord;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.base.BaseCommonPresenter;
import com.bsoft.hospitalchs.model.InspectBean;
import com.bsoft.hospitalchs.model.NOResultBean;
import com.bsoft.hospitalchs.model.RecordInfo;
import com.bsoft.hospitalchs.model.RecordResBean;
import com.bsoft.hospitalchs.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 泅渡者
 * Created on 2017/5/18.
 */

public class ChildrecordPresenter extends BaseCommonPresenter<ChildrecordContract.View> implements ChildrecordContract.Presenter {
    public ChildrecordPresenter(ChildrecordContract.View view, Context context) {
        super(view, context);
    }

    /**
     * 获取初始信息
     */
    @Override
    public void getRecord( RecordInfo recordInfo) {
        mCompositeSubscription.add(apiImple.GetRecordInfo(recordInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RecordResBean>() {
                    @Override
                    public void onCompleted() {
                        Log.e("获取完成", "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("获取失败", e.getMessage());
                    }

                    @Override
                    public void onNext(RecordResBean recordResBean) {

                        if (recordResBean.getCode() == 200) {
                            view.showRecord(recordResBean);
                        }
                    }
                }));
    }

    @Override
    public void toSaveinfo(InspectBean inspectBean) {
        mCompositeSubscription.add(apiImple.SavePreflight(inspectBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {
                        Log.e("保存完成", "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("保存失败", e.getMessage());
                    }

                    @Override
                    public void onNext(JsonObject str) {
                        Gson gson = new Gson();
                        NOResultBean noResultBean = gson.fromJson(str, new TypeToken<NOResultBean>() {
                        }.getType());
                        if (noResultBean.getCode() == 200) {
                            // 创建构建器
                            AlertDialog.Builder builder = new AlertDialog.Builder(context);
                            // 设置参数
                            builder.setTitle(R.string.app_name).setIcon(R.mipmap.ic_launcher)
                                    .setMessage("保存成功！")
                                   .setNegativeButton("", new DialogInterface.OnClickListener() {
                                       @Override
                                       public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                       }
                                   }).create().show();
                        }
                    }
                }));
    }
}
